from DrissionPage import SessionPage
import time
import re
import json
import utils
from py_mini_racer import MiniRacer

class GuanMing:
    def __init__(self):
        self.utils = utils.Utils()
        self.page = SessionPage()
        self.ctx = MiniRacer()
        # 获取token
        self.tokenUrl = "https://zhonghua.gmw.cn/service/getToken.do?callback=jQuery172011993289611962399_1737540926402&_={time}"
    
    # 获取token
    def getToken(self):
        # 从文本获取token的值jQuery172011993289611962399_1737540926402({"token":"ld2NxPnx36F7IO0RnkXwF/gmvWUHB0DDP31hhq0qZ3HgN8mhAjnIgxHhMDaVKk1e"})
        self.page.get(self.tokenUrl.format(time=int(time.time()*1000)))
        token_str = self.page.html
        # 使用正则表达式匹配 token 的值
        pattern = r'"token":"([^"]+)"'
        return re.search(pattern, token_str).group(1)
    
    # 生成获取新闻的json数据
    def getNewsJson(self, keyWords, pages, token_value):        
        # 读取并执行 JavaScript 文件
        with open(r'guangming\guangming.js', 'r', encoding='utf-8') as file:
            js_code = file.read()
            self.ctx.eval(js_code)

        # 调用 JavaScript 函数，并传递参数
        jsonStrUrl = self.ctx.call('pcsearch', keyWords, pages, token_value)
        self.page.get(jsonStrUrl)
        jsonStr = self.page.html
        pattern = r'\?\((.*?)\)'
        matches = re.search(pattern, jsonStr)
        return json.loads(matches.group(1))
    
    # 获取新闻总页数
    def getNewsTotalPage(self, jsonData):
        return jsonData["page"]["totalCount"] / jsonData["page"]["numPerPage"]
    
    # 获取对应的新闻url
    def getNewsUrl(self, jsonData):
        urls = []
        lists = jsonData["result"]["list"]
        for i in lists:
            urls.append(i["url"])
            for j in i["itemList"]:
                urls.append(j["url"])
        return urls
    
    # 获取指定页数的前百分比新闻uRL
    def getNewsByPageUrl(self, keyWords, percent):
        allUrls = []
        token_value = self.getToken()
        jsonData = self.getNewsJson(keyWords, 1, token_value)
        totalPage = self.getNewsTotalPage(jsonData)
        onePageUrls = self.getNewsUrl(jsonData)
        allUrls.extend(onePageUrls)
        needPages = int(totalPage * percent)
        for i in range(2, needPages + 1):
            jsonData = self.getNewsJson(keyWords, i, token_value)
            allUrls.extend(self.getNewsUrl(jsonData))
        
        return allUrls
    
    # 获取指定页数的新闻
    def getNewsByPage(self, keyWords, percent):
        allUrls = self.getNewsByPageUrl(keyWords, percent)
        with open(r'guangming\guangming.txt', 'w', encoding='utf-8') as file:
            for i in allUrls:
                self.page.get(i)
                newPage = self.page.ele(".text_c").text
                file.write(newPage + '\n')
                print(i)

if __name__ == "__main__":
    guanming = GuanMing()
    guanming.getNewsByPage("习近平 福建", 0.01)